import apiClient from './apiClient';
    import socketClient, { SocketClient } from './socketClient';

    /**
     * 格式化日期
     * @param {Date|string|number} date - 日期对象、时间戳或日期字符串
     * @param {string} format - 格式化模板，例如 'YYYY-MM-DD HH:mm:ss'
     * @returns {string} 格式化后的日期字符串
     */
    const formatDate = (date, format = 'YYYY-MM-DD') => {
      const d = new Date(date);
      
      if (isNaN(d.getTime())) {
        return 'Invalid Date';
      }
      
      const year = d.getFullYear();
      const month = String(d.getMonth() + 1).padStart(2, '0');
      const day = String(d.getDate()).padStart(2, '0');
      const hours = String(d.getHours()).padStart(2, '0');
      const minutes = String(d.getMinutes()).padStart(2, '0');
      const seconds = String(d.getSeconds()).padStart(2, '0');
      
      return format
        .replace('YYYY', year)
        .replace('MM', month)
        .replace('DD', day)
        .replace('HH', hours)
        .replace('mm', minutes)
        .replace('ss', seconds);
    };

    /**
     * 格式化文件大小
     * @param {number} bytes - 字节数
     * @param {number} decimals - 小数位数
     * @returns {string} 格式化后的文件大小
     */
    const formatFileSize = (bytes, decimals = 2) => {
      if (bytes === 0) return '0 Bytes';
      
      const k = 1024;
      const dm = decimals < 0 ? 0 : decimals;
      const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
      
      const i = Math.floor(Math.log(bytes) / Math.log(k));
      
      return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
    };

    /**
     * 深拷贝对象
     * @param {Object} obj - 要拷贝的对象
     * @returns {Object} 拷贝后的对象
     */
    const deepClone = (obj) => {
      if (obj === null || typeof obj !== 'object') {
        return obj;
      }
      
      if (obj instanceof Date) {
        return new Date(obj.getTime());
      }
      
      if (obj instanceof Array) {
        return obj.map(item => deepClone(item));
      }
      
      if (obj instanceof Object) {
        const copy = {};
        Object.keys(obj).forEach(key => {
          copy[key] = deepClone(obj[key]);
        });
        return copy;
      }
      
      throw new Error(`无法复制对象: ${obj}`);
    };

    /**
     * 防抖函数
     * @param {Function} func - 要执行的函数
     * @param {number} wait - 等待时间（毫秒）
     * @returns {Function} 防抖处理后的函数
     */
    const debounce = (func, wait = 300) => {
      let timeout;
      
      return function executedFunction(...args) {
        const later = () => {
          clearTimeout(timeout);
          func(...args);
        };
        
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
      };
    };

    /**
     * 节流函数
     * @param {Function} func - 要执行的函数
     * @param {number} limit - 时间限制（毫秒）
     * @returns {Function} 节流处理后的函数
     */
    const throttle = (func, limit = 300) => {
      let inThrottle;
      
      return function executedFunction(...args) {
        if (!inThrottle) {
          func(...args);
          inThrottle = true;
          setTimeout(() => {
            inThrottle = false;
          }, limit);
        }
      };
    };

    /**
     * 生成唯一ID
     * @returns {string} 唯一ID
     */
    const generateUniqueId = () => {
      return Date.now().toString(36) + Math.random().toString(36).substr(2, 9);
    };

    export {
      apiClient,
      socketClient,
      SocketClient,
      formatDate,
      formatFileSize,
      deepClone,
      debounce,
      throttle,
      generateUniqueId,
    };
